import { BaseTool } from "../core/BaseTool";
import { Leafer, Pen, DragEvent } from "leafer-ui";
import { IToolConfig } from "../types";

export class PenTool extends BaseTool {
  readonly name = "pen";
  protected events = [DragEvent.START, DragEvent.DRAG, DragEvent.END];

  private isDrawing = false;
  private currentPen: Pen | null = null;
  constructor(app: Leafer, config: IToolConfig) {
    super(app, config);
    console.log('手动赋值 config');
    (this as any).config = config; // 仅用于快速验证问题
  }

  protected onDragStart(e: DragEvent) {
    if (e.multiTouch) return;
    this.isDrawing = true;

    this.currentPen = new Pen({
      stroke: 'red'||this.config.pen.color,
      strokeWidth: 5||this.config.pen.width,
    });
    this.app.add(this.currentPen);

    const pos = e.getPagePoint();
    console.log(this.currentPen,pos)
    this.currentPen.moveTo(pos.x, pos.y);
  }

  protected onDragMove(e: DragEvent) {
    if (!this.isDrawing || !this.currentPen) return;

    const pos = e.getPagePoint();
    this.currentPen.lineTo(pos.x, pos.y);
  }

  protected onDragEnd() {
    this.isDrawing = false;
    this.currentPen = null;
    // 提交历史记录
    // HistoryManager.commit(this.app.children.slice(-1)[0])
  }

  // 未使用的事件处理器需空实现
  protected onPointerDown() {}
  protected onPointerMove() {}
  protected onPointerUp() {}
}
